package com.yx.yinshanjinniu.base;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;


public class BasePopUpWindow extends PopupWindow{
    private static final String TAG = "BasePopUpWindow";
    private WindowManager.LayoutParams params;
    private View view;
    private int backgoundColor = Color.WHITE;
    protected Activity activity;

    public BasePopUpWindow(Activity a, int resId) {
        activity = a;
        view = a.getLayoutInflater().inflate(resId, null);
        this.setContentView(view);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
//        this.setAnimationStyle(R.style.Animation_PopUpWindow);
        setBackgroundColor(backgoundColor);
        //修改Activity的透明度
        params = a.getWindow().getAttributes();
    }

    public void setBackgroundColor(int color) {
        this.setBackgroundDrawable(new ColorDrawable(color));
    }

    protected View getPopUpView() {
        return view;
    }

    //显示在Activity中部
    public void showCenter() {
        if (activity != null && !activity.isFinishing()) {
            params.alpha = 0.5f;
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//让窗口背景后面的任何东西变暗
            activity.getWindow().setAttributes(params);
            showAtLocation(activity.getWindow().getDecorView().findViewById(android.R.id.content), Gravity.CENTER_HORIZONTAL | Gravity.CENTER_HORIZONTAL, 0, 0);
        }
    }

    //显示在Activity底部
    public void show() {
        if (activity != null && !activity.isFinishing()) {
            params.alpha = 0.5f;
            //让窗口背景后面的任何东西变暗
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            activity.getWindow().setAttributes(params);
            showAtLocation(activity.getWindow().getDecorView().findViewById(android.R.id.content), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        }
    }

    public void showAsDropDown(View anchor) {
        //Android7.0适配
        if (Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    public void showAsDropDown(View anchor,int x,int y) {
        //Android7.0适配
        if (Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor,x,y);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (activity != null) {
            params.alpha = 1.0f;
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            activity.getWindow().setAttributes(params);
        }
    }
}
